import { nmAppErrorLog, nmAppLog } from '@/common/utils';
import { basic } from '@/domain';
import { getQueryParam, httpApi, QueryParamKeys } from '@amcax/base';

/**
 * 导出模型
 * @param fileType 导出文件类型
 * @param object 导出对象dxid
 * @param name 导出文件名
 */
export const cmdExport = async (
  fileType: string,
  object: string[],
  name: string,
) => {
  try {
    // windows包导出使用检测是否有electronApi
    if (window['electronAPI']) {
      const filters = [];
      switch (fileType) {
        case 'step':
          filters.push({
            name: 'STEP Files',
            extensions: ['step'],
          });
          break;
        case 'obj':
          filters.push({
            name: 'OBJ Files',
            extensions: ['obj'],
          });
          break;
      }
      nmAppLog('filters/object:', filters, object);
      window['electronAPI']?.exportModel(filters, object);
    } else {
      // 根据fileType，获取导出路径path和下载链接dl，后端将文件保存至path，然后前端通过dl进行下载
      const documentId = getQueryParam<string>(QueryParamKeys.DOCUMENT_ID);
      const { data } = await httpApi.getExportPath(documentId, fileType);
      const { path, dl } = data;
      await basic.export({ path, object });
      const link = document.createElement('a');
      link.href = dl;
      link.download = `${name}.${fileType}`; // 可以设置下载的文件名
      link.click();
    }
  } catch (error) {
    nmAppErrorLog('导出模型失败', error);
  }
};
